package com.abel.bigwater.dataserver.model.qdies

import com.abel.bigwater.dataserver.model.PacketDataLenException
import io.netty.buffer.ByteBuf
import java.util.*

class DtenAskWarnData : D10BaseData(20) {
    override var type = FuncType.AskWarn

    var state = BitSet(20 * 8)

    override fun fill(buf: ByteBuf) {
        val ba = state.toByteArray()
        if (ba.size >= 20) {
            buf.writeBytes(ba, 0, 20)
        } else {
            buf.writeBytes(ba).writeBytes(ByteArray(20 - ba.size) { 0 })
        }
    }

    override fun parse(buf: ByteBuf): Boolean {
        if (buf.readableBytes() < count()) {
            throw PacketDataLenException("invalid bytes")
        }
        state = BitSet.valueOf(ByteArray(20).apply { buf.readBytes(this) })
        return true
    }

    override fun count(): Int = 20

}
